<?php
include 'lib.php';
ini_set('display_errors', 1);
ini_set('max_execution_time', 500);
$br = '<br/>';

$artists = Database::query("SELECT * FROM `artists` WHERE CHAR_LENGTH(`lastfm_url`) > 0 ORDER BY `lastfm_events_updated` ASC LIMIT 50");

ini_set('max_execution_time', 500);

// Include the API
require 'lib/lastfmapi/lastfmapi.php';


// Put the auth data into an array
$authVars = array(
	'apiKey' => '5e6a9eb30f4dd80779c96113d27b5a25',
    'secret' => '1e0a1293702d76020603e622453b620b'
);
$config = array(
	'enabled' => true,
	'path' => 'lib/lastfmapi/',
	'cache_length' => 1800
);
// Pass the array to the auth class to eturn a valid auth
$auth = new lastfmApiAuth('setsession', $authVars);

// Call for the album package class with auth data
$apiClass = new lastfmApi();
$artistClass = $apiClass->getPackage($auth, 'artist', $config);

foreach ($artists as $artist)
{

    $methodVars = array(
    	'artist' => $artist['name'],
    );
    
    if ($events = $artistClass->getEvents($methodVars) ) {
        var_dump($events);
    }
    else {
    	echo '<b>Error '.$artistClass->error['code'].' - </b><i>'.$artistClass->error['desc'].'</i>';
        continue;
    }
            
    foreach ($events as $i=>$event)
    {
 

    $exists = Database::find("events", array("lastfm_id" => $event['id']));
    if ($exists[0]['id']) continue;
    
    $venue = Database::find("venues", array("name" => $event['venue']['name']));

    
    if ($venue[0]['id']) $venue_id = $venue[0]['id'];
    else 
    {
        $venue_id = Database::save("venues", 
        array(
            'name' => $event['venue']['name'],
            'country' => $event['venue']['location']['country'],
            'city' => $event['venue']['location']['city'],
            'lat' => $event['venue']['location']['geopoint']['lat'],
            'lon' => $event['venue']['location']['geopoint']['long']));
    }    
    
    $event_id = array(
        'artist_id' => $artist['id'],
        'lastfm_id' => $event['id'],
        'venue_id' => $venue_id,
        'summary' => $event['title'],
        'date' => date('Y-m-d', $event['startdate']),
        'updated' => date('Y-m-d H:i:s'),
        'website' => $event['website'],
        'description' => strip_tags($event['description'])
        );

    $event_id = Database::save("events", $event_id);            
    foreach ($event['artists'] as $event_artist)
    {
        echo '<h4>'.$event_artist.'</h4>';
        $event_artist_exists = Database::find("artists", array("display_name" => $event_artist));
        
        if ($aid = $event_artist_exists[0]['id'])
        {
        Database::save("artist_events", array(
            'event_id' => $event_id,
            'artist_id' => $aid));
        }
    }
        
    
        if ($i>=3) break;
    }
    
        $artist_updating = Database::find("artists", array("id" => $artist['id']),"id");
    $artist_updating[0]['lastfm_events_updated'] = date('Y-m-d H:i:s');
    Database::save("artists", $artist_updating[0]);

}

function artistHasTag($artist_tags, $tag_name)
{
    foreach ($artist_tags as $artist_tag)
    {
        if ($tag['tag'] == strtolower($tag_node['name'])) return true;
    }
    
    return false;
}
?>